package com.py.unit.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.py.api.PyCodeApi;
import com.py.unit.domain.PyAccount;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.py.unit.domain.PySuppliers;
import com.py.unit.service.IPySuppliersService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 供应商信息Controller
 * 
 * @author ruoyi
 * @date 2024-11-20
 */
@RestController
@RequestMapping("suppliers")
public class PySuppliersController extends BaseController
{
    @Autowired
    private IPySuppliersService pySuppliersService;
   @Resource
    private PyCodeApi pyCodeApi;

    /**
     *  获取编码
     * @param code
     * @return
     */

    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code",required = false) Integer code){
        System.out.println("进来了"+code);
        return pyCodeApi.generateCode(code);
    }



    /**
     * 查所有不分页
     */
    @RequestMapping("/selectAll")
    public List<PySuppliers> selectAll(){
        System.out.println("进来了");
        return pySuppliersService.selectAll();
    }



    /**
     * 查询供应商信息列表
     */
    @RequiresPermissions("unit:suppliers:list")
    @GetMapping("/list")
    public TableDataInfo list(PySuppliers pySuppliers)
    {
        startPage();
        List<PySuppliers> list = pySuppliersService.selectPySuppliersList(pySuppliers);
        System.out.println("list:"+list);
        System.out.println("财务信息:"+pySuppliers.getPyAccountList());
        return getDataTable(list);
    }

    /**
     * 导出供应商信息列表
     */
    @RequiresPermissions("unit:suppliers:export")
    @Log(title = "供应商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PySuppliers pySuppliers)
    {
        List<PySuppliers> list = pySuppliersService.selectPySuppliersList(pySuppliers);
        ExcelUtil<PySuppliers> util = new ExcelUtil<PySuppliers>(PySuppliers.class);
        util.exportExcel(response, list, "供应商信息数据");
    }

    /**
     * 获取供应商信息详细信息
     */
    @RequiresPermissions("unit:suppliers:query")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable("supplierId") Long supplierId)
    {
        System.out.println("---------------进来了------------");
        return success(pySuppliersService.selectPySuppliersBySupplierId(supplierId));
    }

    /**
     * 新增供应商信息
     */
    @RequiresPermissions("unit:suppliers:add")
    @Log(title = "供应商信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PySuppliers pySuppliers)
    {
//        System.out.println("添加："+pySuppliers);
//        System.out.println("财务113:"+pySuppliers.getPyAccount());
//        System.out.println("number:"+pySuppliers.getNumber());
//        System.out.println("codeId:"+pySuppliers.getCodeId());
        pyCodeApi.saveCode(pySuppliers.getCodeId(),pySuppliers.getNumber());
        pySuppliers.setManager(SecurityUtils.getUsername());
        pySuppliers.setCreatedAt(new Date());
        return toAjax(pySuppliersService.insSupAcc(pySuppliers));
    }
    /**
     * 修改供应商信息
     */
    @RequiresPermissions("unit:suppliers:edit")
    @Log(title = "供应商信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PySuppliers pySuppliers)
    {
        System.out.println("修改供应商"+pySuppliers);
        return toAjax(pySuppliersService.updatePySuppliers(pySuppliers));
    }

    /**
     * 删除供应商信息
     */
    @RequiresPermissions("unit:suppliers:remove")
    @Log(title = "供应商信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds)
    {
        System.out.println(supplierIds);
        return toAjax(pySuppliersService.deletePySuppliersBySupplierIds(supplierIds));
    }


    /**
     * 查询详情
     */
    @GetMapping("/selectPysuppliers")
    public PySuppliers selectPysuppliers(@RequestParam("supplierId") Long supplierId){
        System.out.println(pySuppliersService.selectPySuppliersBySupplierId(supplierId));
        System.out.println("-----进来了2-----");
        return pySuppliersService.selectPySuppliersBySupplierId(supplierId);
    }
}
